package logica.persistencia;

import java.util.LinkedList;
import java.util.List;

import org.jdom.Element;

import xml.ConstantesXml;
import logica.Atributo;
import logica.AtributoSimple;
import logica.SinNombreException;
public class AtributoSimplePersistente extends AtributoPersistente {

	public AtributoSimplePersistente(Atributo atributo) {
		super(atributo);
		
	}
	
	@Override
	public void serializar() {
		Element elementoAtributo = new Element(ConstantesXml.ELEMENTO_ATRIBUTO_SIMPLE);
		AtributoSimple atribSimple = (AtributoSimple)this.getAtributo();
			elementoAtributo.setAttribute(ConstantesXml.ATRIBUTO_TIPO,atribSimple.getTipo());
		super.serializarElementoGenerico(elementoAtributo);
	}


	public void hidratar(Element elementoAtributoSimple) {
		String tipo = elementoAtributoSimple.getAttributeValue(ConstantesXml.ATRIBUTO_TIPO);
		AtributoSimple atribSimple = (AtributoSimple)this.getAtributo();
		atribSimple.setTipo(tipo);
	
	}
	
	public static List<Atributo> hidratarListaAtributosSimples(List <Element> elementListaAtributosSimples) {
		List<Atributo> atributos = new LinkedList<Atributo>();
		for (Element elementoAtributoSimple : elementListaAtributosSimples) {
			String nombreAtributo = elementoAtributoSimple.getAttributeValue(ConstantesXml.ATRIBUTO_NOMBRE);
			AtributoSimple atribS;
			try {
				atribS = new AtributoSimple(nombreAtributo);
				AtributoSimplePersistente atribSP = new AtributoSimplePersistente(atribS);
				atribSP.hidratarGenerico(elementoAtributoSimple);
				atribSP.hidratar(elementoAtributoSimple);
				atributos.add(atribS);
			} catch (SinNombreException e) {
				e.printStackTrace();
			}
		}
		return atributos;
	}

	

}
